import router from '@system.router';
import prompt from '@system.prompt'
export default {
    onInit() {
        //获取全局数据
        this.addressList = this.$app.$def.AddressData.addressList;
        console.log(this.addressList)
    },
    data: {
        title: 'World',
        avatar:"/common/images/moren.png",
        avatar1:"/common/images/people.png",
        edit:"/common/images/edit.png",
        isManage:false,
        currentAdd:0,
        provinceValue:"",
        cityValue:"",
        areaValue:"",
        addressList:[
            {
                id:1,
                name:"shuang",
                phone:"121324324354",
                address:["广东省","广州市","番禺区"],
                addDetail:" 沙湾世良路 1342号广州番禺职业技术学院",
                isDefult:true
            },
            {
                id:2,
                name:"旭晴",
                phone:"121324324354",
                address:["广东省","深圳市","番禺区"],
                addDetail:" 沙湾世良路 1342号广州番禺职业技术学院",
                isDefult:false
            },
            {
                id:3,
                name:"晓萍",
                phone:"121324324354",
                address:["广东省","河源市","番禺区"],
                addDetail:" 沙湾世良路 1342号广州番禺职业技术学院",
                isDefult:false
            },
            {
                id:4,
                name:"丹儿",
                phone:"121324324354",
                address:["广东省","广州市","番禺区"],
                addDetail:" 沙湾世良路 1342号广州番禺职业技术学院",
                isDefult:false
            },
            {
                id:5,
                name:"丹萍",
                phone:"121324324354",
                address:["广东省","广州市","番禺区"],
                addDetail:" 沙湾世良路 1342号广州番禺职业技术学院",
                isDefult:false
            },
            {
                id:6,
                name:"yyl",
                phone:"121324324354",
                address:["广东省","广州市","番禺区"],
                addDetail:" 沙湾世良路 1342号广州番禺职业技术学院",
                isDefult:false
            }
        ],
        itemMsg:{
            id:6,
            name:"名字",
            phone:"电话号码",
            address:["","",""],
            addDetail:"沙湾世良路 1342号广州番禺职业技术学院 ",
            isDefult:false
        },
        province:["广东省"],
        city:["广州市","深圳市","河源市"],
        area:[],
        areaG:["番禺区","天河区","荔湾区","海珠区","增城区","黄埔区","花都区","从化区"],
        areaS: ["龙华区","龙岗区","宝安区","罗湖区","福田区","盐田区","坪山区","南山区","光明区"],
        areaH:  ["源城区","龙门县","紫金县","龙川县","连平县","翁源县","和平县","新丰县"]

    },

    manage(){
        this.isManage=!this.isManage;
        this.addressList.forEach(value => {
            if (value.isDefult==true && value != this.addressList[0]) {
                const arr= this.addressList.shift();
                this.addressList=this.addressList.filter(item => item.isDefult!=true);
                this.addressList.unshift(arr);
                this.addressList.unshift(value);
            }
        })
        //深拷贝新的地址数据
        const addressNew = JSON.parse(JSON.stringify(this.addressList));
        this.addressList = addressNew;
    },
    setDefault(id){
         this.addressList.forEach(value => {
             if (id == value.id) {
                 value.isDefult=true;

             }
             else{
                 value.isDefult=false;
             }
         })
         //深拷贝新的地址数据
         const addressNew = JSON.parse(JSON.stringify(this.addressList));
         this.$element('eventDialog').close()
         this.addressList = addressNew;

    },
    showeventDialog(e) {
        console.log(JSON.stringify("e-----"+e))
        this.currentAdd=e;
        this.addressList.forEach(value => {
            if(e==value.id){
                this.itemMsg.id=value.id;
                this.itemMsg.name=value.name;
                this.itemMsg.phone=value.phone;
                this.itemMsg.address=value.address;
                this.provinceValue=value.address[0];
                this.cityValue=value.address[1];
//                市变地区也变
                if(this.cityValue=="广州市"){
                    this.area=this.areaG
                }else if(this.cityValue=="深圳市"){
                    this.area=this.areaS
                }else if(this.cityValue=="河源市"){
                    this.area=this.areaH
                }
                this.areaValue=value.address[2];
                this.itemMsg.addDetail=value.addDetail;
            }
        });
        this.$element('eventDialog').show()
    },
    setProvince(e){
        this.provinceValue = e.newValue;
        this.cityValue="请选择";
    },
    setCity(e) {
        this.cityValue = e.newValue;
        this.areaValue="请选择";//改变上一级，清空数据
        if(this.cityValue=="广州市"){
            this.area=this.areaG
        }else if(this.cityValue=="深圳市"){
            this.area=this.areaS
        }else if(this.cityValue=="河源市"){
            this.area=this.areaH
        }
        console.log("dsdasd++++"+this.cityValue)
    },
    setArea(e){
        this.areaValue=e.newValue;
    },
    onSubmit(result) {//提交修改地址
        console.log(result.value.name)
        this.addressList.forEach(value => {
            if(this.currentAdd==value.id){
                value.name=result.value.name;
                value.phone=result.value.phone;
                value.address[0]=this.provinceValue;
                value.address[1]=this.cityValue;
                value.address[2]=this.areaValue;
                value.addDetail=result.value.addDetail;
            }
        });
        //深拷贝新的地址数据
        const addressNew = JSON.parse(JSON.stringify(this.addressList));
        this.$element('eventDialog').close();
        this.addressList = addressNew;
    },
    //删除某一条
    deleteOne(id){
        var arr = this.addressList.filter(item => item.id != id)
        this.addressList = arr;
        this.$app.$def.AddressData.addressList= this.addressList;
    },
    //添加一条
    addOne(){
        this.$element('eventDialog2').show()

    },
    submitOne(result){
//        var arr = new Array();
//        console.log( result.
        //        value.name+"------"+result.value.phone+"------"+this.provinceValue+"------"+this.cityValue+"------"+this.areaValue+"------"+result.value.addDetail)
        var obj=new Object();
        obj.name = result.value.name;
        obj.phone = result.value.phone;
        var arr=new Array();
        arr.push(this.provinceValue);
        arr.push(this.cityValue);
        arr.push(this.areaValue);
        obj.address=arr;
//        arr.address[0] = this.provinceValue;
//        arr.address[1] = this.cityValue;
//        arr.address[2] = this.areaValue;
        obj.addDetail = result.value.addDetail;
//        console.log(JSON.stringify(obj));

        this.addressList.push(obj)
        //深拷贝新的地址数据
        const addressNew = JSON.parse(JSON.stringify(this.addressList));
        this.$element('eventDialog').close();
        this.addressList = addressNew;
    }


}
